﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxGroup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListtbGroup(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroup.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách các nhóm" });
            try
            {
                tbGroup_Proxy pr = new tbGroup_Proxy();
                List<DataAccess.tbGroup> list = new List<tbGroup>();
               
                int totalitem = 0;
                if (keyword == "null" || keyword == "")
                {
                    list = pr.SelectAlltbGroup_Proxy();
                }
                else
                {
                    list = (pr.SelectNo_tbGroup_Proxy(keyword)).ToList<tbGroup>();
                }
                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
              
                return new JavaScriptSerializer().Serialize(new { Status = true, Data= list2, TotalItem= totalitem, Currentpage=currentpage});
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false, Data = "" });
            }
        }
        [WebMethod]
        public static string DeletetbGroup(int GroupID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroup.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa nhóm" });
            try
            {
                tbGroup_Proxy pr = new tbGroup_Proxy();
                if (pr.Delete_tbGroup_Proxy(GroupID))
                    return new JavaScriptSerializer().Serialize(new { Status = true });
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string InserttbGroup(string GroupName, string Note)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroup.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm nhóm" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbGroup).Name);
                tbGroup_Proxy pr = new tbGroup_Proxy();
                DataAccess.tbGroup a = new DataAccess.tbGroup();
                a.GroupId = 0;
                a.GroupName = GroupName;
                a.Note = Note;

                if (pr.Insert_tbGroup_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới Introduce thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
        [WebMethod]
        public static string UpdatetbGroup(int GroupID, string GroupName, string Note)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementGroup.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa nhóm" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbIntroduce).Name);
                tbGroup_Proxy pr = new tbGroup_Proxy();
                DataAccess.tbGroup a = new DataAccess.tbGroup();
                a.GroupId = GroupID;
                a.GroupName = GroupName;
                a.Note = Note;
                if (pr.Update_tbGroup_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Cập nhật Introduce thành công");
                return new JavaScriptSerializer().Serialize(new { Status = true });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = false });
            }
        }
    }
}